package com.scott.itcast.thread.lesson03;

/**
 * 同步的例子
 */
public class TraditionalThreadSynchronized {
	
	private void init(){
		final Outputer outputer = new Outputer();
		
		//第1个线程
		new Thread(new Runnable(){
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
						outputer.output("0123456789");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		}).start();
		
		
		//第2个线程
		new Thread(new Runnable(){
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
						outputer.output("asdfghjklqwertyuiop");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		}).start();
	}
	
	public static void main(String[] args) {
		new TraditionalThreadSynchronized().init();
	}
	
	class Outputer{
		private String xxx="";
		
		public void output(String name){
			int len = name.length();
			
			//同步代码块
			synchronized (xxx) {
				for(int i=0;i<len;i++){
					System.out.print(name.charAt(i));
				}
				//换行
				System.out.println();
			}
			
		}
	}

}



